<template>
  <el-dialog
    width="650px"
    custom-class="password-dialog"
    :visible.sync="passwordVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="user" :rules="rules" label-width="200px">
      <el-form-item label="旧默认密码" prop="oldPassword">
        <el-input
          v-model="user.oldPassword"
          placeholder="请输入旧默认密码"
          type="password"
        />
      </el-form-item>
      <el-form-item label="新默认密码" prop="newPassword">
        <el-input
          v-model="user.newPassword"
          placeholder="请输入新默认密码"
          type="password"
        />
      </el-form-item>
      <el-form-item
        label="确认新默认密码"
        prop="confirmPassword"
      >
        <el-input
          v-model="user.confirmPassword"
          placeholder="请确认新默认密码"
          type="password"
        />
      </el-form-item>
      <div class="mt10 mb30 ml60" style="color:red">该操作具有一定的风险性，请务必谨慎操作！！！</div>

      <el-form-item>
        <el-button type="primary" size="mini" @click="submit">保 存</el-button>
        <el-button type="danger" size="mini" @click="close">关 闭</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { updateUsersDefaultPwd } from '@/api/system/sysuser'

export default {
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    // 密码验证
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('请输入密码'))
      } else {
        var passReg = /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![0-9\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]{8,32}$/
        if (!passReg.test(value)) {
          return callback(new Error('密码至少包含大写，小写字母，数字或特殊字符其中三种，长度至少8位（不包含空格）'))
          if (this.user.confirmPassword !== '') {
            this.$refs.form.validateField('confirmPassword')
          }
        }
        callback()
      }
    }
    // 确认密码验证
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('请再次输入新默认密码'))
      }
      var passReg = /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![0-9\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]{8,32}$/
      if (!passReg.test(value)) {
        return callback(new Error('密码至少包含大写，小写字母，数字或特殊字符其中三种，长度至少8位（不包含空格）'))
      }
      setTimeout(() => {
        if (value !== this.user.newPassword) {
          callback(new Error('两次输入新默认密码不一致!'))
        } else if (value == this.user.oldPassword) {
          callback(new Error('新默认密码不可和旧默认密码一致!'))
        } else {
          callback()
        }
      }, 100)
    }
    return {
      passwordVisible: false,
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: '旧默认密码不能为空', trigger: 'blur' }
        ],
        newPassword: [
          {
            trigger: 'blur',
            validator: validatePass
          }
        ],
        confirmPassword: [
          {
            trigger: 'blur',
            validator: validatePass2
          }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          updateUsersDefaultPwd(
            this.user.oldPassword,
            this.user.newPassword
          ).then(response => {
            if (response.code === 200) {
              this.msgSuccess('处理成功')
              this.passwordVisible = false
            } else {
              this.msgError(response.msg)
            }
          })
        }
      })
    },
    open() {
      this.reset()
      this.passwordVisible = true
    },
    reset() {
      this.user = {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      }
    },
    close() {
      this.reset()
      this.passwordVisible = false
    }
  }
}
</script>
